//幻灯
var simSwitch = (function(){
	return {
		slide: slide
		,serialScroll: serialScroll
	}
    function slide(ele, time) {
        var t = time || 3000;
        ele.each(function () {
            var e = ele;
            var btnhtml = ''
            var itmes = e.find('.item');
            var len = itmes.length;
            var btn;
            var operate;
            var operatehtml = '<div class="operate"><a href="javascript:" class="prev"></a><a href="javascript:" class="pause"></a><a href="javascript:" class="next"></a></div>';
            var i = 0;
            var current = 0;
            var settime;
            var is_animate = false;
            for (; i < len; i++) {
                btnhtml += '<a href="javascript:" class="btns_item">' + (i + 1) + '</a>'
            }
            btnhtml = '<div class="btns">' + btnhtml + '</div>';
            if (ele.find('.btns').length === 0) {
                e.append(btnhtml);
                e.append(operatehtml);
            }
            btn = e.find('.btns');
            operate = e.find('.operate');

            function animate(type) {
                if (is_animate) {//动画进行中直接返回
                    return;
                }
                is_animate = true;
                switch (type) {
                    case 'next':
                        if (current < (len - 1)) {
                            current++;
                        } else {
                            current = 0;
                        }

                        break;
                    case 'prev':
                        if (current > 0) {
                            current--;
                        } else {
                            current = len - 1;
                        }
                        break;
                }
                var cur = itmes.eq(current);
				var img = cur.find('img:first');
				var data_src = img.attr('data-src');
				var src = img.attr('src');
				if(!src && data_src){
					img.attr('src', data_src)
				}
                cur.fadeIn(300).siblings('.item').fadeOut(300, function () {
                    is_animate = false;
					btn.children().eq(current).addClass('hover').siblings().removeClass('hover');
                });
                
            }
			init();
            function bindEvent() {
                e.on('mouseleave', function () {
                    play();
                }).trigger('mouseleave');
                e.on('mouseenter', function () {
                    stop();
                });
                e.find('.next').on('click', function () {
                    animate('next');
                });
                e.find('.prev').on('click', function () {
                    animate('prev');
                });
                btn.children().on('click', function () {
                    var e = $(this);
                    var i = e.index();
                    current = i - 1;
                    animate('next');
                });

            }
			function init(){
				btn.children(':first').addClass('hover');
				itmes.eq(0).fadeIn().find('img').attr('src', function(){
					return $(this).attr('data-src');
				});
				bindEvent();
			}
            function stop() {
                settime && clearInterval(settime);
            }
            function play() {
                stop();
                settime = setTimeout(function () {
                    animate('next');
                    play();
                }, t);
            }
        });
    }
	function serialScroll(ele, num, time){
		var t = time || 3000;
		var is_animate = false;
		var container = ele.children(':first');
		var items = container.children();
		var items_len = items.length;
		var item_width = items.eq(0).outerWidth(true);
		var scroll_width = item_width * num;
		var current = num;
		var switch_ele;
		var direction;
		var mix_num = num - items_len % num;
		var settime;
		init();
		function init(){
			bindEvent();
			mix_num !== num ? mix() : '';
			loadImg(items.slice(0, num).find('img'));
		}
		//无缝调整
		function mix(){
			var cp = items.slice(0, mix_num).clone();
			items.filter(':last').after(cp);
			items = container.children();
			items_len = items.length;
		}
		function animate(type){
			if(is_animate){
				return;
			}
			is_animate = true;
			switch(type){
				case 'next':
					if(current < items_len){
						current = current + num;
						direction = '-=' + scroll_width;
					}else{
						current = num;
						direction = 0;
					}
				break;
				case 'prev':
					
					if(current > num){
						current = current - num;
						direction = '+=' + scroll_width;
					}else{
						switch_ele = items.slice(-num);
						current = items_len;
						direction = -item_width * (items_len - num);
					}
				break;
			}
			switch_ele = items.slice(current - num, current);
			loadImg(switch_ele.find('img'));
			container.animate({
				left: direction
			}, 300, function(){
				is_animate = false;
			});
		}
		function bindEvent(){
			ele.on('click', '.caro_next', function(){
				animate('next');
			});
			ele.on('click', '.caro_prev', function(){
				animate('prev');
			});
			ele.on('mouseleave', function () {
				play();
			}).trigger('mouseleave');
			ele.on('mouseenter', function () {
				stop();
			});
		}
		function stop() {
			settime && clearInterval(settime);
		}
		function play() {
			stop();
			settime = setTimeout(function () {
				animate('next');
				play();
			}, t);
		}
		function loadImg(imgs){
			imgs.each(function(){
				var img = $(this);
				if(!img.attr('src')){
					img.attr('src', img.attr('data-src'));
				}
			});
		}
	}
})();
